import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { ActionSheetConfig } from 'ngx-weui';
import { ApiService } from 'src/app/core/api';
import { StorageService } from 'src/app/core/sotrage/storage.service';
import { GB } from '../../../environments/environment';
import { I18nService } from '../../core/i18n/i18n.service';

@Component({
	templateUrl: './index.component.html',
	styleUrls: ['./index.less'],
})
export class BaseMsgComponent implements OnInit {
	isEmail = true;
	constructor(
		private i: I18nService,
		private activedRoute: ActivatedRoute,
		private router: Router,
		private fb: FormBuilder,
		private api: ApiService,
		private sto: StorageService) {
		// this.i.get(['MSG.register-code', 'MSG.register-user']).subscribe(res => {
		// 	this.msgData = res;
		// });
	}
	pageType = 1;
	tabs = ['基本信息', '结算账户信息'];
	config: ActionSheetConfig = {
		title: 'Language changes',
	} as ActionSheetConfig;
	position = 'top';
	gb = GB;
	ob;
	res: any = {
		id: '',
		mail: '',
		mobile: '',
		vcode: '',
		password: '',
		rpassword: '',
		worldpost: '1',
	};
	registerEmailForm: FormGroup;
	bankForm: FormGroup;
	e: number;
	msgData: any;
	user;

	ngOnInit() {
		this.configForm();
		this.getBankData();
	}

	configForm() {
		this.user = this.sto.get('user');
		this.registerEmailForm = this.fb.group(
			{
				name: [this.user.name, [Validators.required]],
				mobile: [this.user.mobile, [Validators.required]]
			}
		);
		this.bankForm = this.fb.group(
			{
				bankName: ['', [Validators.required]],
				bankAccountNum: ['', [Validators.required]],
				bankAccountName: ['', [Validators.required]],
				mobile: ['', [Validators.required]],
				bankSubBranch: ['', [Validators.required]]
			}
		);

		this.registerEmailForm.setErrors({
			email: false
		});

		// }
	}

	onChangType(bool) {
		this.isEmail = bool;
		this.configForm();
	}

	onSaveBase(f) {
		const data = f.value;
		this.api.post('编辑用户基本信息', data).then(res => {
			this.router.navigate(['/']);
		});
	}
	bankData;
	getBankData(){
		this.api.post('用户银行信息详情', {}).then(res => {
			this.bankData = res['data']['data'];
			this.bankForm.setValue({
				bankAccountName:this.bankData.bankAccountName,
				bankName:this.bankData.bankName,
				bankSubBranch:this.bankData.bankSubBranch,
				bankAccountNum:this.bankData.bankAccountNum,
				mobile:this.bankData.mobile
			})
		});
	}
	onSaveBank(f) {
		const data = f.value;
		this.api.post('编辑用户银行信息', data).then(res => {
			this.getBankData();
		});
	}
}

